home *** CD-ROM | disk | FTP | other *** search
/ .net 2002 March / DotNetMagazine-Issue107-Coverdisc-NET107-02-03-PCMac.bin / pc / PC Software / picks / HTTrack / httrack-3.22-3.exe / {app} / src_win / WinHTTrack / HTTrackInterface.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-30  |  5.4 KB  |  255 lines

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6.  
  7. #include "htsglobal.h"
  8. #include "htsbase.h"
  9.  
  10. #include "HTTrackInterface.h"
  11.  
  12. // Lecture d'une ligne (peut Ítre unicode ‡ priori)
  13. int linput(FILE* fp,char* s,int max) {
  14.   int c;
  15.   int j=0;
  16.   do {
  17.     c=fgetc(fp);
  18.     if (c!=EOF) {
  19.       switch(c) {
  20.         case 13: break;  // sauter CR
  21.         case 10: c=-1; break;
  22.         case 9: case 12: break;  // sauter ces caractËres
  23.         default: s[j++]=(char) c; break;
  24.       }
  25.     }
  26.   }  while((c!=-1) && (c!=EOF) && (j<(max-1)));
  27.   s[j]='\0';
  28.   return j;
  29. }
  30. int linput_trim(FILE* fp,char* s,int max) {
  31.   int rlen=0;
  32.   char* ls=(char*) malloct(max+2);
  33.   s[0]='\0';
  34.   if (ls) {
  35.     char* a;
  36.     // lire ligne
  37.     rlen=linput(fp,ls,max);
  38.     if (rlen) {
  39.       // sauter espaces et tabs en fin
  40.       while( (rlen>0) && ((ls[max(rlen-1,0)]==' ') || (ls[max(rlen-1,0)]=='\t')) )
  41.         ls[--rlen]='\0';
  42.       // sauter espaces en dÈbut
  43.       a=ls;
  44.       while((rlen>0) && ((*a==' ') || (*a=='\t'))) {
  45.         a++;
  46.         rlen--;
  47.       }
  48.       if (rlen>0) {
  49.         memcpy(s,a,rlen);      // can copy \0 chars
  50.         s[rlen]='\0';
  51.       }
  52.     }
  53.     //
  54.     freet(ls);
  55.   }
  56.   return rlen;
  57. }
  58. int linput_cpp(FILE* fp,char* s,int max) {
  59.   int rlen=0;
  60.   s[0]='\0';
  61.   do {
  62.     int ret;
  63.     if (rlen>0)
  64.     if (s[rlen-1]=='\\')
  65.       s[--rlen]='\0';      // couper \ final
  66.     // lire ligne
  67.     ret=linput_trim(fp,s+rlen,max-rlen);
  68.     if (ret>0)
  69.       rlen+=ret;
  70.   } while((s[max(rlen-1,0)]=='\\') && (rlen<max));
  71.   return rlen;
  72. }
  73.  
  74. // idem avec les car spÈciaux
  75. void rawlinput(FILE* fp,char* s,int max) {
  76.   int c;
  77.   int j=0;
  78.   do {
  79.     c=fgetc(fp);
  80.     if (c!=EOF) {
  81.       switch(c) {
  82.         case 13: break;  // sauter CR
  83.         case 10: c=-1; break;
  84.         default: s[j++]=(char) c; break;
  85.       }
  86.     }
  87.   }  while((c!=-1) && (c!=EOF) && (j<(max-1)));
  88.   s[j++]='\0';
  89. }
  90.  
  91. // Like linput, but in memory (optimized)
  92. int binput(char* buff,char* s,int max) {
  93.   char* end;
  94.   int count;
  95.  
  96.   // clear buffer
  97.   s[0]='\0';
  98.   // end of buffer?
  99.   if ( *buff == '\0')
  100.     return 1;
  101.   // find ending \n
  102.   end=strchr(buff,'\n');
  103.   // ..or end of buffer
  104.   if (!end)
  105.     end=buff+strlen(buff);
  106.   // then count number of bytes, maximum=max
  107.   count=min(max,end-buff);
  108.   // and strip annoying ending cr
  109.   while( (count>0) && (buff[count] == '\r'))
  110.     count--;
  111.   // copy
  112.   if (count > 0) {
  113.     strncatbuff(s, buff, count);
  114.   }
  115.   // and terminate with a null char
  116.   s[count]='\0';
  117.   // then return the supplemental jump offset
  118.   return (end-buff)+1;
  119.  
  120. int fexist(char* s) {
  121.   FILE* fp;
  122.   fp=fopen(s,"rb");
  123.   if (fp!=NULL) fclose(fp);
  124.   return (fp!=NULL);
  125.  
  126. int fsize(char* s) {
  127.   FILE* fp;
  128.   fp=fopen(s,"rb");
  129.   if (fp!=NULL) {
  130.     int i;
  131.     fseek(fp,0,SEEK_END);
  132.     i=ftell(fp);
  133.     fclose(fp);
  134.     return i;
  135.   } else return -1;
  136. }
  137.  
  138. TStamp time_local(void) {
  139.   return ((TStamp) time(NULL));
  140. }
  141.  
  142.  
  143.  
  144.  
  145.  
  146. #define NOSTATIC_RESERVE(name, type, nelt) NOSTATIC_XRESERVE(name, type, nelt)
  147. #define NOSTATIC_XRESERVE(name, type, nelt) do { \
  148.   __declspec( thread ) static type thValue[nelt]; \
  149.   __declspec( thread ) int  static initValue = 0; \
  150.   name = thValue; \
  151.   if (!initValue) { \
  152.     initValue = 1; \
  153.     memset(&thValue, 0, sizeof(thValue)); \
  154.   } \
  155. } while(0)
  156.  
  157.  
  158.  
  159. typedef struct {
  160.   char buff[16][HTS_URLMAXSIZE*2*2];
  161.   int rol;
  162. } concat_strc;
  163. char* concat(const char* a,const char* b) {
  164.   concat_strc* strc;
  165.   NOSTATIC_RESERVE(strc, concat_strc, 1);
  166.   strc->rol=((strc->rol+1)%16);    // roving pointer
  167.   strcpybuff(strc->buff[strc->rol],a);
  168.   if (b) strcatbuff(strc->buff[strc->rol],b);
  169.   return strc->buff[strc->rol];
  170. }
  171. // conversion fichier / -> antislash
  172. #if HTS_DOSNAME
  173. char* __fconv(char* a) {
  174.   int i;
  175.   for(i=0;i<(int) strlen(a);i++)
  176.     if (a[i]=='/')  // convertir
  177.       a[i]='\\';
  178.   return a;
  179. }
  180. char* fconcat(char* a,char* b) {
  181.   return __fconv(concat(a,b));
  182. }
  183. char* fconv(char* a) {
  184.   return __fconv(concat(a,""));
  185. }
  186. #endif
  187.  
  188. /* / et \\ en / */
  189. char* __fslash(char* a) {
  190.   int i;
  191.   for(i=0;i<(int) strlen(a);i++)
  192.     if (a[i]=='\\')  // convertir
  193.       a[i]='/';
  194.   return a;
  195. }
  196. char* fslash(char* a) {
  197.   return __fslash(concat(a,""));
  198. }
  199.  
  200. // conversion minuscules, avec buffer
  201. char* convtolower(char* a) {
  202.   concat_strc* strc;
  203.   NOSTATIC_RESERVE(strc, concat_strc, 1);
  204.   strc->rol=((strc->rol+1)%16);    // roving pointer
  205.   strcpybuff(strc->buff[strc->rol],a);
  206.   hts_lowcase(strc->buff[strc->rol]);  // lower case
  207.   return strc->buff[strc->rol];
  208. }
  209.  
  210. // conversion en minuscules
  211. void hts_lowcase(char* s) {
  212.   int i;
  213.   for(i=0;i<(int) strlen(s);i++)
  214.     if ((s[i]>='A') && (s[i]<='Z'))
  215.       s[i]+=('a'-'A');
  216. }
  217.  
  218. char* next_token(char* p,int flag) {
  219.   int detect=0;
  220.   int quote=0;
  221.   p--;
  222.   do {
  223.     p++;
  224.     if (flag && (*p=='\\')) {   // sauter \x ou \"
  225.       if (quote) {
  226.         char c='\0';
  227.         if (*(p+1)=='\\')
  228.           c='\\';
  229.         else if (*(p+1)=='"')
  230.           c='"';
  231.         if (c) {
  232.           char tempo[8192];
  233.           tempo[0]=c; tempo[1]='\0';
  234.           strcatbuff(tempo,p+2);
  235.           strcpybuff(p,tempo);
  236.         }
  237.       }
  238.     }
  239.     else if (*p==34) {  // guillemets (de fin)
  240.       quote=!quote;
  241.     }
  242.     else if (*p==32) {
  243.       if (!quote)
  244.         detect=1;
  245.     }
  246.     else if (*p=='\0') {
  247.       p=NULL;
  248.       detect=1;
  249.     }
  250.   } while(!detect);
  251.   return p;
  252. }
  253.